package com.woniuxy.banka.service.impl;

import com.woniuxy.banka.mapper.BankAMapper;
import com.woniuxy.banka.service.BankAService;
import com.woniuxy.commons.entity.Bank;
import com.woniuxy.commons.service.BankBService;
import com.woniuxy.commons.util.ResponseResult;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author zhangyong
 * @Date 2021/8/9 0009下午 3:40
 */
@Service
public class BankAServiceImpl implements BankAService {

    @Resource
    private BankAMapper bankAMapper;
    @Resource
    private BankBService bankBService;

    @GlobalTransactional  //全局事务管理：用在事务发起方
    @Override
    public int update() {
        //1.让bankb执行加钱的操作
        ResponseResult responseResult = bankBService.update(2001, 100);
        //判断结果
        if (responseResult.getMessage().equals("success")) {
            Bank src = new Bank(1001, 100, "zhangsan");
            //减钱
            bankAMapper.update(src);
            //制造除0异常看是否回滚，两个都不成功：事务的原子性，要么都成功，要么都失败
            //System.out.println(1/0);
        }
        return 1;
    }
}
